\section{Streams}
\label{sec:streams}
Streams in C++ are often used to perform I/O, such as writing to a file or printing to the console (output), or read data from a file or from use input through keyboard (input).
Streams are, however, more general than just I/O.
Streams can be used to internal communication between threads, by linking an \texttt{istream} to an \texttt{ostream} through a shared buffer.
Streams can also be used for formatting data object as strings of character or the other way around by using a \texttt{stringstream}~\cite[p.~640-641]{stroustrup2000} -- although this is in practice usually in combination with some form of I/O.

As explained in \secref{sec:intro} I/O is often preformed by using the put to and get from operators.
Streams also have member functions which can be used to perform I/O and to modify the state of the stream, which I will explain further in \subref{sub:streamManip}.
The most basic output functions for an \texttt{ostream} are \texttt{put} and \texttt{write}, which take a \texttt{char}, and a \texttt{char *} and an \texttt{int} respectively.

The put to operator takes two operands, the left one being \texttt{ostream} -- strictly speaking a programmer can choose another type of object.
The operator is overloaded to take a number of different types of inputs as second operand such as \texttt{char}, \texttt{int}, and \texttt{char *}~\cite[p.~610]{stroustrup2000}, this is further described for user defined types in \subref{sub:overloadOps}.


\subsection{Overloading Operators}
\label{sub:overloadOps}
It is very useful to be able to overload the put to and get from operators for user defined types.
Even for types in the standard library this is used.
An \texttt{ostream} for instance does not have a definition of the put to operator for the type \texttt{string}, although this indeed seem very usable.
The initial respond to this problem might be to introduce a dependency from streams to strings, but this would not work user defined types, since the standard library should never have dependencies toward user produced code -- then it would not be a standard library.
Instead the overloading of the put to operator is placed together with the definition of the user defined type -- in this case \texttt{string}.
This also applies for the get from operator for \texttt{istream}~\cite[p.~598]{stroustrup2000}.

%Namespace resolution of operators.

\subsection{Stream Manipulators}
\label{sub:streamManip}
There are different ways with which to specify how formatting should be performed when inputting/outputting from a stream.
What I find most interesting are stream manipulators.
The formatting options in a stream are mainly regarding formatting of numbers; these include the precision of floats, base of the number, whether or not to show the base (for output), case of letters (for hexadecimal and scientific notation).
%This has lead me to focus on the formatting of numbers as well, in particular integers.
%An alternative way to format integers is shown in \secref{sec:prog}.
There are also options which are more general, such as the minimum width of the next output operation, character used to pad, and where to pad.

Most options are kept on each stream as a set of flags.
However, options such as the precision of float, the padding character (called \texttt{fill}), and minimum width of next output operation must be stored separately.
To set options member functions of the stream can be used, such as:
\begin{lstlisting}[style=code, float=false]
cout.width(10);
cout.fill('_');
cout.setf(ios_base::hex,ios_base::basefield);
cout.setf(ios_base::showbase);
cout << 42; // Prints: '______0x2a'
\end{lstlisting}
This seem a bit tedious, having to write \texttt{cout.} before every call.
In my opinion some form of chaining would be much nicer.
Also the \texttt{setf} might be a little tricky to understand, especially where it takes two arguments; the first argument is the flag which you wish to set, the second is a mask.
The flag and the mask must match in order for the option to change.
In this case it is the ``base'' option which I am changing, therefore the \texttt{ios\_base::basefield} must be given as the second argument to the call.
The reason this extra parameter is needed is that there are actually three bits controlling which base is used, hence when the bit indicating hexadecimal is set the other two (indicating octal and decimal) should be unset.
This unsetting of bits is performed by means of the mask~\cite[p.~627-628]{stroustrup2000}.
The usage of a bit map to store flags and masks to set and unset is quite efficient, but the interface should not expose it as such, if writability and readability are highly prioritized.
A much clearer way to do this is trough stream manipulators as follows:
\begin{lstlisting}[style=code, float=false]
cout<< setw(10) 
    << setfill('_') 
    << setbase(16) 
    << setiosflags(ios_base::showbase) 
    << 42 << endl; // Prints: '______0x2a'
\end{lstlisting}
Here we obtain the same result, but with chaining of the $<<$ operator and different stream manipulators.
We avoid the use of the obscure bit mask to set the base, but are still required to use a flag setter to show the base.

A thing that I find peculiar, for both usage of member function and stream manipulators, is that most options apply until changed, but width only apply for the next output operation.
On one hand I think that this is bad practice to have one option which does not behave as the others, but on the other hand \texttt{width} is somewhat special in that you probably do not want to output two objects with the same width.
Of course this depends on the application being developed.

\subsection{The \texttt{basic} Streams}
\label{sub:basicStreams}
Both \texttt{ostream} and \texttt{istream} have a \texttt{basic} counterpart called \texttt{basic\_ostream} and \texttt{basic\_istream} respectively.
These \texttt{basic} streams are templates, with two type parameters; the first is the character type used by the stream, the second is the character trait used by the stream.
In fact \texttt{ostream} and \texttt{istream} are simply \texttt{typedef}s of their \texttt{basic} counter parts.
They use the character type \texttt{char} and character trait \texttt{char\_traits$<$char$>$}.

I have not worked with other instantiations of the \texttt{basic} streams during this project, but thought it a valuable information to bring in order to show the generality of the streams in the standard library.



%\subsection{Class Hierarchy}
%\label{sub:streamHier}
%Both \texttt{ostream} and \texttt{istream} are part of a class hierarchy.
%he topmost base class in this hierarchy are 

